package com.wangzhenyu.common.sdk

import android.content.Context
import android.content.Intent
import android.nfc.Tag
import android.os.Bundle
import android.util.Log
import cn.jpush.android.api.CmdMessage
import cn.jpush.android.api.CustomMessage
import cn.jpush.android.api.JPushInterface
import cn.jpush.android.api.JPushMessage
import cn.jpush.android.api.NotificationMessage
import cn.jpush.android.service.JPushMessageReceiver
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils
import com.wangzhenyu.common.base.BaseActivity
import com.wangzhenyu.common.base.IConstant
import com.wangzhenyu.common.util.VoiceUtils
import com.wangzhenyu.common.util.log.LogUtil


class MyJPushReceiver : JPushMessageReceiver() {

    companion object {
        private const val TAG = "PushMessageReceiver"
    }

    //收到自定义消息回调
    override fun onMessage(p0: Context?, p1: CustomMessage?) {

    }

    //收到通知回调
    override fun onNotifyMessageArrived(
        context: Context?,
        message: NotificationMessage
    ) {
        try {
            val jPushExtra: JPushExtra? =
                GsonUtils.fromJson(message.notificationExtras, JPushExtra::class.java)
            LogUtil.d(TAG, jPushExtra.toString())
            jPushExtra?.let {
                if (IConstant.MESSAGE_ORDER_PAY_SUCCESS == it.type) {
                    //新的订单
                    VoiceUtils.playVoiceOfNewOrder(Utils.getApp())
                } else if (IConstant.MESSAGE_ORDER_CANCEL == it.type) {
                    //订单取消
                    VoiceUtils.playVoiceOfOrderCancel(Utils.getApp())
                }
            }
            if (jPushExtra?.type == IConstant.MESSAGE_RIDER_NOT_APPLY) {
                //骑手长时间不接单
//                val i = Intent(context, DialogActivity::class.java)
//                i.putExtra("ORDER_SN", jPushExtra.orderSN)
//                i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
//                context?.startActivity(i)
//                MyApp.getInstance()
//                    .startActivity(Intent(MyApp.getInstance(), DialogActivity::class.java))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    override fun onNotifyMessageOpened(
        context: Context,
        message: NotificationMessage
    ) {
        try {
            //打开自定义的Activity
            var intent = Intent(context, Class.forName("com.wzy.business.store.main.MainActivity"))
            val bundle = Bundle()
            bundle.putString(JPushInterface.EXTRA_NOTIFICATION_TITLE, message.notificationTitle)
            bundle.putString(JPushInterface.EXTRA_ALERT, message.notificationContent)
            val jPushExtra: JPushExtra? =
                GsonUtils.fromJson(message.notificationExtras, JPushExtra::class.java)
            jPushExtra?.let {
                if (IConstant.MESSAGE_ORDER_PAY_SUCCESS == jPushExtra.type) {
                    //支付成功
                    bundle.putString(IConstant.JPUSH_TYPE, IConstant.MESSAGE_ORDER_PAY_SUCCESS)
                } else if (IConstant.MESSAGE_APPLY_SHOP_REFUSE == jPushExtra.type) {
                    intent = Intent(
                        context,
                        Class.forName("com.wzy.business.store.main.ShopWaitApplyActivity")
                    )
                }
            }
            intent.putExtras(bundle)
            //Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag.
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
            context.startActivity(intent)
        } catch (throwable: Throwable) {
            throwable.printStackTrace()
        }
    }

    //通知的MultiAction回调
    override fun onMultiActionClicked(context: Context?, intent: Intent) {
        //        Log.e(TAG, "[onMultiActionClicked] 用户点击了通知栏按钮");
        //        String nActionExtra = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA);
        //
        //        //开发者根据不同 Action 携带的 extra 字段来分配不同的动作。
        //        if (nActionExtra == null) {
        //            Log.d(TAG, "ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null");
        //            return;
        //        }
        //        if (nActionExtra.equals("my_extra1")) {
        //            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮一");
        //        } else if (nActionExtra.equals("my_extra2")) {
        //            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮二");
        //        } else if (nActionExtra.equals("my_extra3")) {
        //            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮三");
        //        } else {
        //            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮未定义");
        //        }
    }

    //清除通知回调
    override fun onNotifyMessageDismiss(
        context: Context?,
        message: NotificationMessage
    ) {
        LogUtils.i(TAG, "[onNotifyMessageDismiss] $message")
    }

    //注册成功回调
    override fun onRegister(
        context: Context?,
        registrationId: String
    ) {
        LogUtils.i(TAG, "[onRegister] $registrationId")
    }

    //长连接状态回调
    override fun onConnected(
        context: Context?,
        isConnected: Boolean
    ) {
        LogUtils.i(TAG, "[onConnected] $isConnected")
    }

    //注册失败回调
    override fun onCommandResult(
        context: Context?,
        cmdMessage: CmdMessage
    ) {
        LogUtils.d(TAG, "[onCommandResult] $cmdMessage")
    }

    //tag 增删查改的操作会在此方法中回调结果
    override fun onTagOperatorResult(
        context: Context?,
        jPushMessage: JPushMessage?
    ) {
        //  TagAliasOperatorHelper.getInstance().onTagOperatorResult(context, jPushMessage)
        super.onTagOperatorResult(context, jPushMessage)
    }

    //查询某个 tag 与当前用户的绑定状态的操作会在此方法中回调结果
    override fun onCheckTagOperatorResult(
        context: Context?,
        jPushMessage: JPushMessage?
    ) {
        //TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context, jPushMessage)
        super.onCheckTagOperatorResult(context, jPushMessage)
    }

    //alias 相关的操作会在此方法中回调结果。
    override fun onAliasOperatorResult(p0: Context?, p1: JPushMessage?) {
        //TagAliasOperatorHelper.getInstance("").onAliasOperatorResult(p0, p1)
        super.onAliasOperatorResult(p0, p1)
    }

    //设置手机号码会在此方法中回调结果
    override fun onMobileNumberOperatorResult(
        context: Context?,
        jPushMessage: JPushMessage?
    ) {
        //TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context, jPushMessage)
        super.onMobileNumberOperatorResult(context, jPushMessage)
    }

    override fun onNotificationSettingsCheck(
        context: Context?,
        isOn: Boolean,
        source: Int
    ) {
        super.onNotificationSettingsCheck(context, isOn, source)
        LogUtils.i(TAG, "[onNotificationSettingsCheck] isOn:$isOn,source:$source")
    }
}